/**  
 * All rights Reserved, Designed By <a href="www.itttq.com" target="_blank">www.itttq.com</a>
 * @date:   2018年1月2日 下午4:42:56
 * @Copyright: 2018 <a href="www.itttq.com" target="_blank">www.itttq.com</a> Inc. All rights reserved. 
 */  
package com.itttq.gshop.pbpm.provider;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.jdbc.SQL;

import com.itttq.gshop.domain.po.OrderRefundPo;
import com.itttq.gshop.pbpm.cmpt.mybatis.CustomerSQL;

/**   
 * @ClassName:  OrderRefundPoSqlProvider
 * @author: 迷蝶
 * @date:   2018年1月2日 下午4:42:56 
 * @Description: TODO
 *     
 * @Copyright: 2018 <a href="www.itttq.com" target="_blank">www.itttq.com</a> Inc. All rights reserved. 
 */
public class OrderRefundSqlProvider {
	public static final String TABLE_NAME = "order_refund";

	/**
	 * 查找的SQL语句组装<br>
	 * 约定：分页两个参数为：start - 起始位置； size- 每页数量
	 * 
	 * @param orderRefund
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String findOrderRefunds(final OrderRefundPo orderRefund) {
		return new CustomerSQL() {{
			SELECT("amount,amount_pay,amount_total,audit_result,batch,create_time,description,id,operator_id,operator_name,order_code,order_id,order_time,pay_method_id,pay_method_name,pay_time,type,update_time,updater_id,user_id,user_name ");
			FROM(TABLE_NAME);
			
			if (null != orderRefund.getAmount()) {
				WHERE("amount=#{amount}");
			}
			if (null != orderRefund.getAmountPay()) {
				WHERE("amount_pay=#{amountPay}");
			}
			if (null != orderRefund.getAmountTotal()) {
				WHERE("amount_total=#{amountTotal}");
			}
			if (null != orderRefund.getAuditResult()) {
				WHERE("audit_result=#{auditResult}");
			}
			if (StringUtils.isNotBlank(orderRefund.getBatch())) {
				WHERE("batch=#{batch}");
			}
			if (StringUtils.isNotBlank(orderRefund.getDescription())) {
				WHERE("description=#{description}");
			}
			if (null != orderRefund.getOperatorId()) {
				WHERE("operator_id=#{operatorId}");
			}
			if (StringUtils.isNotBlank(orderRefund.getOperatorName())) {
				WHERE("operator_name=#{operatorName}");
			}
			if (StringUtils.isNotBlank(orderRefund.getOrderCode())) {
				WHERE("order_code=#{orderCode}");
			}
			if (null != orderRefund.getOrderId()) {
				WHERE("order_id=#{orderId}");
			}
			if (null != orderRefund.getOrderTime()) {
				WHERE("order_time=#{orderTime}");
			}
			if (null != orderRefund.getPayMethodId()) {
				WHERE("pay_method_id=#{payMethodId}");
			}
			if (StringUtils.isNotBlank(orderRefund.getPayMethodName())) {
				WHERE("pay_method_name=#{payMethodName}");
			}
			if (null != orderRefund.getPayTime()) {
				WHERE("pay_time=#{payTime}");
			}
			if (null != orderRefund.getType()) {
				WHERE("type=#{type}");
			}
			if (null != orderRefund.getUpdaterId()) {
				WHERE("updater_id=#{updaterId}");
			}
			if (null != orderRefund.getUserId()) {
				WHERE("user_id=#{userId}");
			}
			if (StringUtils.isNotBlank(orderRefund.getUserName())) {
				WHERE("user_name=#{userName}");
			}
			if(StringUtils.isNotBlank(orderRefund.getSortField())) {
				ORDER_BY(orderRefund.getSortField() + (StringUtils.isNotBlank(orderRefund.getSortMethod()) ? "desc" : orderRefund.getSortField()));
			} else {
				ORDER_BY("id desc");
			}
			if(null != orderRefund.getStart() && null != orderRefund.getSize()) {
				LIMIT(orderRefund.getStart(), orderRefund.getSize());
			}
		}}.toString();
	}
	
	/**
	 * 查找记录总数的SQL语句组装
	 * 
	 * @param orderRefund
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String findOrderRefundCount(final OrderRefundPo orderRefund) {
		return new SQL() {{
			SELECT("count(*)");
			FROM(TABLE_NAME);
			if (null != orderRefund.getAmount()) {
				WHERE("amount=#{amount}");
			}
			if (null != orderRefund.getAmountPay()) {
				WHERE("amount_pay=#{amountPay}");
			}
			if (null != orderRefund.getAmountTotal()) {
				WHERE("amount_total=#{amountTotal}");
			}
			if (null != orderRefund.getAuditResult()) {
				WHERE("audit_result=#{auditResult}");
			}
			if (StringUtils.isNotBlank(orderRefund.getBatch())) {
				WHERE("batch=#{batch}");
			}
			if (StringUtils.isNotBlank(orderRefund.getDescription())) {
				WHERE("description=#{description}");
			}
			if (null != orderRefund.getOperatorId()) {
				WHERE("operator_id=#{operatorId}");
			}
			if (StringUtils.isNotBlank(orderRefund.getOperatorName())) {
				WHERE("operator_name=#{operatorName}");
			}
			if (StringUtils.isNotBlank(orderRefund.getOrderCode())) {
				WHERE("order_code=#{orderCode}");
			}
			if (null != orderRefund.getOrderId()) {
				WHERE("order_id=#{orderId}");
			}
			if (null != orderRefund.getOrderTime()) {
				WHERE("order_time=#{orderTime}");
			}
			if (null != orderRefund.getPayMethodId()) {
				WHERE("pay_method_id=#{payMethodId}");
			}
			if (StringUtils.isNotBlank(orderRefund.getPayMethodName())) {
				WHERE("pay_method_name=#{payMethodName}");
			}
			if (null != orderRefund.getPayTime()) {
				WHERE("pay_time=#{payTime}");
			}
			if (null != orderRefund.getType()) {
				WHERE("type=#{type}");
			}
			if (null != orderRefund.getUpdaterId()) {
				WHERE("updater_id=#{updaterId}");
			}
			if (null != orderRefund.getUserId()) {
				WHERE("user_id=#{userId}");
			}
			if (StringUtils.isNotBlank(orderRefund.getUserName())) {
				WHERE("user_name=#{userName}");
			}
		}}.toString();
	}
	
	/**
	 * 根据ID查找的SQL语句组装
	 * 
	 * @param id
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String findOrderRefundById(final Integer id) {
		return new SQL() {{
			SELECT("*");
			FROM(TABLE_NAME);
			WHERE("id = #{id}");
		}}.toString();
	}
	
	/**
	 * 增加的SQL语句组装
	 * 
	 * @param orderRefund
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String addOrderRefund(final OrderRefundPo orderRefund) {
		return new SQL() {{
			INSERT_INTO(TABLE_NAME);
			if (null != orderRefund.getAmount()) {
				VALUES("amount","#{amount}");
			}
			if (null != orderRefund.getAmountPay()) {
				VALUES("amount_pay","#{amountPay}");
			}
			if (null != orderRefund.getAmountTotal()) {
				VALUES("amount_total","#{amountTotal}");
			}
			if (null != orderRefund.getAuditResult()) {
				VALUES("audit_result","#{auditResult}");
			}
			if (StringUtils.isNotBlank(orderRefund.getBatch())) {
				VALUES("batch","#{batch}");
			}
			if (StringUtils.isNotBlank(orderRefund.getDescription())) {
				VALUES("description","#{description}");
			}
			if (null != orderRefund.getOperatorId()) {
				VALUES("operator_id","#{operatorId}");
			}
			if (StringUtils.isNotBlank(orderRefund.getOperatorName())) {
				VALUES("operator_name","#{operatorName}");
			}
			if (StringUtils.isNotBlank(orderRefund.getOrderCode())) {
				VALUES("order_code","#{orderCode}");
			}
			if (null != orderRefund.getOrderId()) {
				VALUES("order_id","#{orderId}");
			}
			if (null != orderRefund.getOrderTime()) {
				VALUES("order_time","#{orderTime}");
			}
			if (null != orderRefund.getPayMethodId()) {
				VALUES("pay_method_id","#{payMethodId}");
			}
			if (StringUtils.isNotBlank(orderRefund.getPayMethodName())) {
				VALUES("pay_method_name","#{payMethodName}");
			}
			if (null != orderRefund.getPayTime()) {
				VALUES("pay_time","#{payTime}");
			}
			if (null != orderRefund.getType()) {
				VALUES("type","#{type}");
			}
			if (null != orderRefund.getUpdaterId()) {
				VALUES("updater_id","#{updaterId}");
			}
			if (null != orderRefund.getUserId()) {
				VALUES("user_id","#{userId}");
			}
			if (StringUtils.isNotBlank(orderRefund.getUserName())) {
				VALUES("user_name","#{userName}");
			}
		}}.toString();
	}

	/**
	 * 修改的SQL语句组装
	 * 
	 * @param orderRefund
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String updateOrderRefund(final OrderRefundPo orderRefund) {
		return new SQL() {{
			UPDATE(TABLE_NAME);
			
			if (null != orderRefund.getAmount()) {
				SET("amount=#{amount}");
			}
			if (null != orderRefund.getAmountPay()) {
				SET("amount_pay=#{amountPay}");
			}
			if (null != orderRefund.getAmountTotal()) {
				SET("amount_total=#{amountTotal}");
			}
			if (null != orderRefund.getAuditResult()) {
				SET("audit_result=#{auditResult}");
			}
			if (StringUtils.isNotBlank(orderRefund.getBatch())) {
				SET("batch=#{batch}");
			}
			if (StringUtils.isNotBlank(orderRefund.getDescription())) {
				SET("description=#{description}");
			}
			if (null != orderRefund.getOperatorId()) {
				SET("operator_id=#{operatorId}");
			}
			if (StringUtils.isNotBlank(orderRefund.getOperatorName())) {
				SET("operator_name=#{operatorName}");
			}
			if (StringUtils.isNotBlank(orderRefund.getOrderCode())) {
				SET("order_code=#{orderCode}");
			}
			if (null != orderRefund.getOrderId()) {
				SET("order_id=#{orderId}");
			}
			if (null != orderRefund.getOrderTime()) {
				SET("order_time=#{orderTime}");
			}
			if (null != orderRefund.getPayMethodId()) {
				SET("pay_method_id=#{payMethodId}");
			}
			if (StringUtils.isNotBlank(orderRefund.getPayMethodName())) {
				SET("pay_method_name=#{payMethodName}");
			}
			if (null != orderRefund.getPayTime()) {
				SET("pay_time=#{payTime}");
			}
			if (null != orderRefund.getType()) {
				SET("type=#{type}");
			}
			if (null != orderRefund.getUpdaterId()) {
				SET("updater_id=#{updaterId}");
			}
			if (null != orderRefund.getUserId()) {
				SET("user_id=#{userId}");
			}
			if (StringUtils.isNotBlank(orderRefund.getUserName())) {
				SET("user_name=#{userName}");
			}
			WHERE("id = #{id}");
		}}.toString();
	}
	
	/**
	 * 根据ID删除的SQL语句组装
	 * 
	 * @param id
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String delOrderRefund(final Integer id) {
		return new SQL() {{
			DELETE_FROM(TABLE_NAME);
			WHERE("id = #{id}");
		}}.toString();
	}
}
